package com.tads.torry.messagecenter.domain.accessor.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tads.torry.messagecenter.domain.enums.UserStatus;
import com.tads.torry.messagecenter.infras.entity.TenantDO;
import com.tads.torry.messagecenter.domain.model.Tenant;
import com.tads.torry.messagecenter.infras.mapper.TenantMapper;
import com.tads.torry.messagecenter.domain.accessor.TenantAccessor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.zen.boot.starter.mybatisplus.service.AccessorImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author torry.wang
 * @since 2025-03-12
 */
@Service
@SuppressWarnings("DuplicatedCode")
public class TenantAccessorImpl extends AccessorImpl<TenantMapper, TenantDO, Tenant> implements TenantAccessor {
    @Override
    public Tenant toModel(TenantDO entity) {
        if (entity == null) return null;

        Tenant tenant = new Tenant();
        tenant.setUsername(entity.getUsername());
        tenant.setEmail(entity.getEmail());
        tenant.setPassword(entity.getPassword());
        tenant.setApiKey(entity.getApiKey());
        tenant.setSecretKey(entity.getSecretKey());
        tenant.setStatus(UserStatus.parse(entity.getStatus()));
        tenant.setDescr(entity.getDescr());
        tenant.setId(entity.getId());
        tenant.setCreateTime(entity.getCreateTime());
        tenant.setUpdateTime(entity.getUpdateTime());
        return tenant;
    }

    @Override
    public TenantDO toEntity(Tenant model) {
        if (model == null) return null;

        TenantDO tenantDO = new TenantDO();
        tenantDO.setUsername(model.getUsername());
        tenantDO.setEmail(model.getEmail());
        tenantDO.setPassword(model.getPassword());
        tenantDO.setApiKey(model.getApiKey());
        tenantDO.setSecretKey(model.getSecretKey());
        tenantDO.setStatus(model.getStatus() == null ? null : model.getStatus().getCode());
        tenantDO.setDescr(model.getDescr());
        tenantDO.setId(model.getId());
        tenantDO.setCreateTime(model.getCreateTime());
        tenantDO.setUpdateTime(model.getUpdateTime());
        return tenantDO;
    }

    @Override
    public Tenant getByApiKey(String apiKey) {
        if (StringUtils.isBlank(apiKey)) return null;
        return getOne(new LambdaQueryWrapper<TenantDO>().eq(TenantDO::getApiKey, apiKey));
    }
}
